package com.csce.domain.entity.go_score.dto;

import com.csce.domain.entity.go_score.GoScoreExam;
import com.csce.domain.entity.go_score.GoScoreExamPlace;
import com.csce.domain.entity.go_score.bo.GoScoreExamDtoIdsBo;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.doudou.core.container.ListUtil;
import com.doudou.core.date.DateUtil;
import com.osce.common.utils.ValidatorUtils;
import lombok.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 妇产科考试对象 go_score_exam
 *
 * @author zzx
 * @since 2024-05-17
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
@ToString
public class GoScoreExamDto implements Serializable {


    /**
     * 主键
     */
    private Long id;

    /**
     * 名称
     */
    @NotBlank(message = "名称不可为空")
    private String name;

    /**
     * 日期
     */
    @JsonFormat(pattern = DateUtil.yyyyMMdd)
    @NotNull(message = "日期不可为空")
    private Date date;

    /**
     * 时间
     */
    @JsonFormat(pattern = DateUtil.HHmmss)
    @NotEmpty(message = "时间不可为空")
    @Size(min = 2,max = 2,message = "时间不可为空")
    private Date[] timeRange;


    /**
     * 科室编号
     */
    @NotNull(message = "科室不可为空")
    private Long deptId;
    private String deptName;

    /**
     * 类型
     */
    private Integer type;

    /**
     * 状态
     */
    private Integer status;

    /**
     * 妇产科考试-考场信息
     */
    @NotEmpty(message = "考场不可为空")
    @Size(min = 1,message = "考场不可为空")
    private List<GoScoreExamPlaceDto> goScoreExamPlaceList;


    public GoScoreExam convertGoScoreExam(){
        GoScoreExam goScoreExam = new GoScoreExam();
        //基本属性
        goScoreExam.setId(this.id);
        goScoreExam.setName(this.name);
        goScoreExam.setDate(this.date);
        goScoreExam.setStartTime(this.timeRange[0]);
        goScoreExam.setEndTime(this.timeRange[1]);
        goScoreExam.setDeptId(this.deptId);
        goScoreExam.setType(this.type);
        goScoreExam.setStatus(this.status);
        List<GoScoreExamPlace> goScoreExamPlaceList = ListUtil.linkedList();
        if (ListUtil.isNotEmpty(this.goScoreExamPlaceList)){
            for (GoScoreExamPlaceDto placeDto : this.goScoreExamPlaceList) {
                ValidatorUtils.validate(placeDto);//手动触发校验
                GoScoreExamPlace goScoreExamPlace = placeDto.convertGoScoreExamPlace();
                goScoreExamPlaceList.add(goScoreExamPlace);
            }
        }
        goScoreExam.setGoScoreExamPlaceList(goScoreExamPlaceList);
        return goScoreExam;
    }


    public GoScoreExamDtoIdsBo getAllIds(){
        GoScoreExamDtoIdsBo goScoreExamDtoIdsBo = new GoScoreExamDtoIdsBo();
        if (ListUtil.isNotEmpty(this.goScoreExamPlaceList)){
            for (GoScoreExamPlaceDto placeDto : this.goScoreExamPlaceList) {
                //场地
                goScoreExamDtoIdsBo.addPlace(placeDto.getPlaceId());
                //技能
                goScoreExamDtoIdsBo.addSkill(placeDto.getSkillId());
                //评分表
                if (ListUtil.isNotEmpty(placeDto.getSheetIds())){
                    goScoreExamDtoIdsBo.addAllSheetId(placeDto.getSheetIds());
                }
                //所有用户，包括学生、教师
                if (ListUtil.isNotEmpty(placeDto.getStudentIdList())){
                    goScoreExamDtoIdsBo.addAllUserId(placeDto.getStudentIdList());
                }
                if (ListUtil.isNotEmpty(placeDto.getTeacherIds())){
                    goScoreExamDtoIdsBo.addAllUserId(placeDto.getTeacherIds());
                }
            }
        }
        return goScoreExamDtoIdsBo;
    }
}
